home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / zoo / amiga.c < prev    next >
C/C++ Source or Header  |  1980-01-02  |  16KB  |  732 lines

  1. #ifndef LINT
  2. static char    amigaid[] = "@(#) amiga.c 1.1 91/08/03 23:21:00";
  3.  
  4. #endif                /* LINT */
  5.  
  6. /*
  7.  * machine.c for Amiga.
  8.  *
  9.  * This file is (C) Copyright 1991 by Olaf Seibert. All rights reserved. May
  10.  * be used and distributed under the same conditions as the other
  11.  * non-public-domain files that are part of Zoo.
  12.  */
  13.  
  14. #undef TEXT            /* typedef in exec/types.h */
  15. #include <assert.h>
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18. #include <signal.h>
  19.  
  20. #ifdef __STDC__
  21. #define __args
  22. #include <functions.h>
  23. #else
  24. BPTR        Lock(), CreateDir(), ParentDir();
  25. long        Examine(), ExNext();
  26. void           *AllocMem();
  27. struct MsgPort *DeviceProc();
  28. long        SetSignal();
  29.  
  30. #endif
  31.  
  32. int iswild    PARMS((char *));
  33. void endfile    PARMS((int));
  34. void fcbpath    PARMS((char *, char *));
  35. void ToDateStamp PARMS((struct DateStamp * datestamp, unsigned date, unsigned time));
  36. void ToMSDate    PARMS((unsigned *date, unsigned *time, struct DateStamp * datestamp));
  37. void _abort    PARMS((void));
  38. long Chk_Abort    PARMS((void));
  39.  
  40. /*
  41.  * WARNING:  This file assumes that ZOOFILE is a standard buffered file.
  42.  * It will have to be modified if ZOOFILE is changed to be an unbuffered
  43.  * file descriptor or to any other kind of file.
  44.  */
  45.  
  46. #ifdef UNBUF_IO
  47. /*
  48.  * Function tell() returns the current seek position for a file
  49.  * descriptor. 4.3BSD on VAX-11/785 has an undocumented tell() function
  50.  * but it may not exist on all implementations, so we code one here to be
  51.  * on the safe side.  It is needed for unbuffered I/O only.
  52.  */
  53. long lseek    PARMS((int, long, int));
  54. long
  55. tell(fd)
  56. int        fd;
  57. {
  58.     return lseek(fd, 0L, 1);
  59. }
  60.  
  61. #endif
  62.  
  63.  
  64. /*
  65.  * Function zootrunc() truncates a file.
  66.  */
  67.  
  68. int
  69. zootrunc(f)
  70. ZOOFILE     f;
  71. {
  72.     fflush(f);                  /* just in case it matters */
  73.     /* Cannot truncate on AmigaOS < 2.0 */
  74. }
  75.  
  76. /*
  77.  * Function fixfname() converts the supplied filename to a syntax legal
  78.  * for the host system.  It is used during extraction.
  79.  */
  80.  
  81. char           *
  82. fixfname(fname)
  83. char           *fname;
  84. {
  85.     return fname;        /* default is no-op */
  86. }
  87.  
  88. /*
  89.  * Function isuadir() returns 1 if the supplied name is a directory, else
  90.  * it returns 0.
  91.  */
  92.  
  93. int
  94. isuadir(path)
  95. char           *path;
  96. {
  97.     struct FileInfoBlock *fib;
  98.     int         its_a_dir;
  99.  
  100.     its_a_dir = 0;
  101.     if (fib = malloc(sizeof (*fib))) {
  102.     BPTR        lock;
  103.  
  104.     if (lock = Lock(path, ACCESS_READ)) {
  105.         Examine(lock, fib);
  106.         its_a_dir = (fib->fib_DirEntryType > 0);
  107.         UnLock(lock);
  108.     }
  109.     free(fib);
  110.     }
  111.     return its_a_dir;
  112. }
  113.  
  114. /* No standard UNIX-compatible time routines */
  115.  
  116. /*
  117.  * Function getutime() gets the date and time of the file name supplied.
  118.  * Date and time is in MSDOS format.
  119.  */
  120. int
  121. getutime(path, date, time)
  122. char           *path;
  123. unsigned       *date,
  124.            *time;
  125. {
  126.     struct FileInfoBlock *fib;
  127.  
  128.     *date = 0;
  129.     *time = 0;
  130.  
  131.     if (fib = malloc(sizeof (*fib))) {
  132.     BPTR        lock;
  133.  
  134.     if (lock = Lock(path, ACCESS_READ)) {
  135.         Examine(lock, fib);
  136.  
  137.         ToMSDate(date, time, &fib->fib_Date);
  138.         UnLock(lock);
  139.     }
  140.     free(fib);
  141.     }
  142.     return 0;
  143. }
  144.  
  145. /*
  146.  * Function setutime() sets the date and time of the filename supplied.
  147.  * Date and time is in MSDOS format.
  148.  */
  149.  
  150. int
  151. setutime(path, date, time)
  152. char           *path;
  153. unsigned int    date,
  154.         time;
  155. {
  156.     struct DateStamp DateStamp;
  157.     extern int SetFileDate PARMS((char *file, struct DateStamp * date));
  158.  
  159.     ToDateStamp(&DateStamp, date, time);
  160.  
  161.     return !SetFileDate(path, &DateStamp);
  162. }
  163.  
  164. #ifdef AZTEC_C
  165. # ifndef  EXEC_MEMORY_H
  166. #    include <exec/memory.h>
  167. # endif
  168. # ifndef ACTION_SET_DATE
  169. #    define ACTION_SET_DATE        34L
  170. # endif
  171.  
  172. int
  173. SetFileDate(path, date)
  174. char           *path;
  175. struct DateStamp *date;
  176. {
  177.     struct MsgPort *task;
  178.     BPTR        lock,
  179.             plock;
  180.     UBYTE       *bcplstring;
  181.     int         result;
  182.  
  183.     if (!(bcplstring = (UBYTE *) AllocMem(64L, (long) MEMF_PUBLIC)))
  184.     return 0;
  185.     result = 0;
  186.     if (!(task = DeviceProc(path))) {
  187.     goto abort;
  188.     }
  189.     if (!(lock = Lock(path, SHARED_LOCK))) {
  190.     goto abort;
  191.     }
  192.     plock = ParentDir(lock);
  193.     UnLock(lock);
  194.  
  195.     /* Strip pathnames first */
  196.     strcpy((char *) (bcplstring + 1), nameptr(path));
  197.     *bcplstring = strlen((char *) (bcplstring + 1));
  198.  
  199.     result = dos_packet(task, ACTION_SET_DATE, NULL, plock,
  200.               (ULONG) bcplstring >> 2, (ULONG) date, 0L, 0L, 0L);
  201.  
  202.     UnLock(plock);
  203. abort:
  204.     FreeMem((void *) bcplstring, 64L);
  205.     return result;
  206. }
  207.  
  208. #endif
  209.  
  210.  
  211. /* No standard UNIX-specific file attribute routines */
  212.  
  213. /*
  214.  * Get file attributes.  Currently only the lowest nine of the *IX mode
  215.  * bits are used.  Also we return bit 23=0 and bit 22=1, which means use
  216.  * portable attribute format, and use attribute value instead of using
  217.  * default at extraction time.
  218.  */
  219.  
  220. unsigned long
  221. getfattr(f)
  222. ZOOFILE     f;
  223. {
  224.     return NO_FATTR;        /* inaccessible -- no attributes */
  225. }
  226.  
  227. /*
  228.  * Set file attributes.  Only the lowest nine bits are used.
  229.  */
  230.  
  231. int
  232. setfattr(f, a)
  233. char           *f;        /* filename */
  234. unsigned long    a;        /* atributes to set */
  235. {
  236.     return 0;            /* cannot do */
  237. }
  238.  
  239. int
  240. iswild(name)
  241. char           *name;
  242. {
  243.     return (strchr(name, '*') != NULL) || (strchr(name, '?') != NULL);
  244. }
  245.  
  246. #define FMAX    3        /* Number of different filename patterns */
  247.  
  248.  
  249. /* #include "various.h" */
  250. /* #include "zoo.h" *//* solely to define PATHSIZE */
  251.  
  252. #ifdef SPECNEXT
  253. /*-
  254.  
  255. nextfile() returns the name of the next source file matching a filespec.
  256.  
  257. INPUT
  258.    what: A flag specifying what to do. If "what" is 0, nextfile()
  259.       initializes itself.     If "what" is 1, nextfile() returns the next
  260.       matching filename.
  261.    filespec:  The filespec, usually containing wildcard characters, that
  262.       specifies which files are needed.  If "what" is 0, filespec must be
  263.       the filespec for which matching filenames are needed.  If "what" is 1,
  264.       nextfile() does not use "filespec" and "filespec" should be NULL to
  265.       avoid an assertion error during debugging.
  266.    fileset:  nextfile() can keep track of more than one set of filespecs.
  267.       The fileset specifies which filespec is being matched and therefore
  268.       which set of files is being considered.  "fileset" can be in the
  269.       range 0:FMAX.   Initialization of one fileset does not affect the
  270.       other filesets.
  271.  
  272. OUTPUT
  273.    IF what == 0 THEN
  274.       return value is NULL
  275.    ELSE IF what == 1 THEN
  276.       IF a matching filename is found THEN
  277.      return value is pointer to matching filename including supplied path
  278.       ELSE
  279.      IF at least one file matched previously but no more match THEN
  280.         return value is NULL
  281.      ELSE IF supplied filespec never matched any filename THEN
  282.         IF this is the first call with what == 1 THEN
  283.            return value is pointer to original filespec
  284.         ELSE
  285.         return value is NULL
  286.         END IF
  287.      END IF
  288.        END IF
  289.    END IF
  290.  
  291. NOTE
  292.  
  293.    Initialization done when "what"=0 is not dependent on the correctness
  294.    of the supplied filespec but simply initializes internal variables
  295.    and makes a local copy of the supplied filespec.  If the supplied
  296.    filespec was illegal, the only effect is that the first time that
  297.    nextfile() is called with "what"=1, it will return the original
  298.    filespec instead of a matching filename.       That the filespec was
  299.    illegal will become obvious when the caller attempts to open the
  300.    returned filename for input/output and the open attempt fails.
  301.  
  302. USAGE HINTS
  303.  
  304. nextfile() can be used in the following manner:
  305.  
  306.       char *filespec;              -- will point to filespec
  307.       char *this_file;              -- will point to matching filename
  308.       filespec = parse_command_line();    -- may contain wildcards
  309.       FILE *stream;
  310.  
  311.       nextfile (0, filespec, 0);          -- initialize fileset 0
  312.       while ((this_file = nextfile(1, (char *) NULL, 0)) != NULL) {
  313.      stream = fopen (this_file, "whatever");
  314.      if (stream == NULL)
  315.         printf ("could not open %s\n", this_file);
  316.      else
  317.          perform_operations (stream);
  318.       }
  319. */
  320.  
  321. static int    first_time[FMAX + 1];
  322. static BPTR    filelock[FMAX + 1];
  323. static struct FileInfoBlock *fib[FMAX + 1];
  324.  
  325. char           *
  326. nextfile(what, filespec, fileset)
  327. int        what;        /* whether to initialize or match     */
  328. register char  *filespec;    /* filespec to match if initializing     */
  329. register int    fileset;    /* which set of files             */
  330. {
  331.     static char     pathholder[FMAX + 1][PATHSIZE];    /* holds a pathname to
  332.                              * return */
  333.     static char     saved_fspec[FMAX + 1][PATHSIZE];    /* our own copy of
  334.                              * filespec   */
  335.  
  336.     long        success;
  337.     char       *ph_fs;
  338.     BPTR        fl_fs;
  339.     struct FileInfoBlock *fib_fs;
  340.  
  341.     assert(fileset >= 0 && fileset <= FMAX);
  342.     ph_fs = pathholder[fileset];
  343.     fl_fs = filelock[fileset];
  344.     fib_fs = fib[fileset];
  345.     if (what == 0) {
  346.     assert(filespec != NULL);
  347.     strcpy(saved_fspec[fileset], nameptr(filespec));        /* save the filespec */
  348.     strcpy(ph_fs, filespec);/* save the path */
  349.     first_time[fileset] = 1;
  350.     return NULL;
  351.     }
  352.     assert(what == 1);
  353.     assert(filespec == NULL);
  354.     assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  355.  
  356.     success = DOSFALSE;
  357.     if (first_time[fileset]) {  /* first time -- initialize etc. */
  358.     filelock[fileset] = fl_fs = 0;
  359.     if (!iswild(saved_fspec[fileset])) {
  360.         goto notwild;
  361.     }
  362.     if (fib[fileset] = fib_fs = malloc(sizeof (struct FileInfoBlock))) {
  363.         char       *np,
  364.                 n;
  365.  
  366.         np = nameptr(ph_fs);
  367.         n = *np;
  368.         *np = '\0';
  369.         if (filelock[fileset] = fl_fs = Lock(ph_fs, ACCESS_READ)) {
  370.         Examine(fl_fs, fib_fs);
  371.         assert(fib->fib_DirEntryType > 0);
  372.         }
  373.         *np = n;
  374.     }
  375.     }
  376.     /* find first or next matching file; skip directories (type > 0) */
  377.     if (fib_fs) {
  378.     for (;;) {
  379.         success = ExNext(fl_fs, fib_fs);
  380.         if (success == DOSFALSE)
  381.         break;
  382.         if (fib_fs->fib_DirEntryType > 0)
  383.         continue;
  384.         if (match_half(fib_fs->fib_FileName, saved_fspec[fileset]))
  385.         break;
  386.     }
  387.     }
  388.     if (success == DOSFALSE) {  /* if error status */
  389. notwild:
  390.     if (first_time[fileset]) {      /* if file never matched then */
  391.         first_time[fileset] = 0;
  392.         return ph_fs;    /* return original filespec */
  393.     } else {        /* else */
  394.         endfile(fileset);
  395.         return NULL;    /* return NULL for no more   */
  396.     }
  397.     } else {            /* a file matched */
  398.     first_time[fileset] = 0;
  399.     /* add path info     */
  400.     strcpy(nameptr(ph_fs), fib_fs->fib_FileName);
  401.     return ph_fs;        /* matching path  */
  402.     }
  403.     /* NOTREACHED */
  404. }                /* nextfile */
  405.  
  406. void
  407. endfile(fileset)
  408. int        fileset;
  409. {
  410.     assert(fileset >= 0 && fileset <= FMAX);
  411.     first_time[fileset] = 0;
  412.     if (fib[fileset]) {
  413.     free(fib[fileset]);
  414.     fib[fileset] = NULL;
  415.     }
  416.     if (filelock[fileset]) {
  417.     UnLock(filelock[fileset]);
  418.     filelock[fileset] = 0;
  419.     }
  420. }
  421.  
  422. #endif
  423.  
  424. char           *
  425. mktemp(name)
  426. char           *name;
  427. {
  428.     char       *x;
  429.  
  430.     if (x = strchr(name, 'X')) {
  431.     struct DateStamp ds;
  432.     long        rnd;
  433.     BPTR        lock;
  434.  
  435.     DateStamp(&ds);
  436.     rnd = (ds.ds_Days << 17) + (ds.ds_Minute << 6) + ds.ds_Tick;
  437.  
  438. retry:
  439.     rnd += (rnd >> 24);
  440.     sprintf(x, "%06lx", rnd & 0x00ffffff);
  441.     if (lock = Lock(name, ACCESS_READ)) {
  442.         UnLock(lock);
  443.         rnd--;
  444.         goto retry;
  445.     }
  446.     }
  447.     return name;
  448. }
  449.  
  450. #ifdef AZTEC_C
  451. # ifndef __STDC__
  452. /*
  453.  * A bit of a rough (and rude) implementation of realloc
  454.  */
  455. void           *
  456. realloc(oldptr, size)
  457. char           *oldptr;
  458. unsigned int    size;
  459. {
  460.     char       *newptr;
  461.  
  462.     newptr = malloc(size);
  463.     if (newptr) {
  464.     CopyMem(oldptr, newptr, (long) size);
  465.     free(oldptr);
  466.     }
  467.     return newptr;
  468. }
  469.  
  470. # endif
  471. #endif
  472.  
  473. void
  474. makedir(name)
  475. char           *name;
  476. {
  477.     BPTR        lock;
  478.  
  479.     if (lock = CreateDir(name)) {
  480.     UnLock(lock);
  481.     }
  482. }
  483.  
  484. #define BASEYEAR         1978
  485. #define DAYS_PER_YEAR         365
  486. #define HOURS_PER_DAY         24
  487. #define MINUTES_PER_HOUR     60
  488. #define SECONDS_PER_MINUTE  60
  489.  
  490. #define DAYS_PER_WEEK         7
  491. #define MONTHS_PER_YEAR      12
  492.  
  493. #define MINUTES_PER_DAY      (MINUTES_PER_HOUR * HOURS_PER_DAY)
  494. #define SECONDS_PER_DAY      ((long) SECONDS_PER_MINUTE * \
  495.                   MINUTES_PER_HOUR * HOURS_PER_DAY)
  496.  
  497. #define DATE_MIN         0x21    /* 1-jan-80 */
  498.  
  499. #define LeapYear(year)  ((year & 3) == 0)       /* From 1-Mar-1901 to
  500.                          * 28-Feb-2100 */
  501.  
  502. int        daycount[MONTHS_PER_YEAR] = {
  503.     31, 28, 31, 30, 31, 30,
  504.     31, 31, 30, 31, 30, 31
  505. };
  506.  
  507. void
  508. ToDateStamp(datestamp, date, time)
  509. struct DateStamp *datestamp;
  510. unsigned    date;
  511. unsigned    time;
  512. {
  513.     {
  514.     int        hours,
  515.             minutes,
  516.             seconds;
  517.  
  518.     seconds = (time & 31) * 2;
  519.     time >>= 5;
  520.     minutes = time & 63;
  521.     time >>= 6;
  522.     hours = time;
  523.  
  524.     datestamp->ds_Minute = MINUTES_PER_HOUR * hours + minutes;
  525.     datestamp->ds_Tick = TICKS_PER_SECOND * seconds;
  526.     }
  527.  
  528.     {
  529.     unsigned    i,
  530.             j,
  531.             t;
  532.     int        year,
  533.             month,
  534.             day;
  535.  
  536.     if (date < DATE_MIN)
  537.         date = DATE_MIN;
  538.  
  539.     day = date & 31;
  540.     date >>= 5;
  541.     month = (date & 15) - 1;
  542.     date >>= 4;
  543.     year = date + 1980;
  544.  
  545.     if ((unsigned) month > 11 ||
  546.         (unsigned) day > (unsigned) daycount[month]) {
  547.         day = 31;
  548.         month = 11;
  549.         year = 1979;
  550.     }
  551.     j = year - BASEYEAR;
  552.  
  553.     /*
  554.      * Get the next lower full leap period (4 years and a day) since
  555.      * ...
  556.      */
  557.     t = (year - BASEYEAR) & ~3;
  558.     i = t;
  559.     t = (t / 4) * (4 * DAYS_PER_YEAR + 1);
  560.  
  561.     /* t now is the number of days in 4 whole years since ... */
  562.  
  563.     while (i < j) {
  564.         t += DAYS_PER_YEAR;
  565.         if (LeapYear(i + BASEYEAR)) {
  566.         t++;
  567.         }
  568.         i++;
  569.     }
  570.  
  571.     /* t now is the number of days in whole years since ... */
  572.  
  573.     for (i = 0; i < month; i++) {
  574.         t += daycount[i];
  575.         if (i == 1 && LeapYear(year)) {
  576.         t++;
  577.         }
  578.     }
  579.  
  580.     /* t now is the number of days in whole months since ... */
  581.  
  582.     t += day - 1;
  583.  
  584.     /* t now is the number of days in whole days since ... */
  585.  
  586.     datestamp->ds_Days = t;
  587.     }
  588. }
  589.  
  590. void
  591. ToMSDate(date, time, datestamp)
  592. unsigned       *date;
  593. unsigned       *time;
  594. register struct DateStamp *datestamp;
  595. {
  596.     {
  597.     unsigned short    hours,
  598.             minutes,
  599.             seconds;
  600.  
  601.     hours = datestamp->ds_Minute / MINUTES_PER_HOUR;
  602.     minutes = datestamp->ds_Minute % MINUTES_PER_HOUR;
  603.     seconds = datestamp->ds_Tick / TICKS_PER_SECOND;
  604.  
  605.     *time = (hours << 11) | (minutes << 5) | (seconds / 2);
  606.     }
  607.     {
  608.     register long    days,
  609.             i,
  610.             t;
  611.     int        year,
  612.             month,
  613.             day;
  614.  
  615.     days = datestamp->ds_Days;
  616.  
  617.     year = BASEYEAR + (days / (4 * DAYS_PER_YEAR + 1)) * 4;
  618.     days %= 4 * DAYS_PER_YEAR + 1;
  619.     while (days) {
  620.         t = DAYS_PER_YEAR;
  621.         if (LeapYear(year))
  622.         t++;
  623.         if (days < t)
  624.         break;
  625.         days -= t;
  626.         year++;
  627.     }
  628.     days++;
  629.     for (i = 0; i < MONTHS_PER_YEAR; i++) {
  630.         t = daycount[i];
  631.         if (i == 1 && LeapYear(year))
  632.         t++;
  633.         if (days <= t)
  634.         break;
  635.         days -= t;
  636.     }
  637.     month = i + 1;
  638.     day = days;
  639.  
  640.     *date = ((year - 1980) << 9) | (month << 5) | day;
  641.     }
  642. }
  643.  
  644. void spec_init    PARMS((void));
  645. void
  646. spec_init()
  647. {
  648. #ifndef NOSIGNAL
  649.     signal(SIGINT, _abort);
  650. #endif
  651. }
  652.  
  653. void
  654. zooexit(code)
  655. int        code;
  656. {
  657.     int         i;
  658.  
  659. #ifdef SPECNEXT
  660.     for (i = 0; i <= FMAX; i++)
  661.     endfile(i);
  662. #endif
  663.  
  664.     exit(code);
  665. }
  666.  
  667. /*
  668.  * called by DB debugger and on CTRL-C.
  669.  */
  670. void
  671. _abort()
  672. {
  673.     zooexit(20);
  674. }
  675.  
  676. #ifdef NEED_SIGNAL
  677.  
  678. /*
  679.  * A minimalistic signal implementation, that ignores the signal number,
  680.  * which is assumed to be SIGINT.
  681.  */
  682.  
  683. static void    (*Handler) PARMS((int));
  684.  
  685. void
  686. (*signal(sig, handler)) PARMS((int))
  687. int        sig;
  688. void        (*handler) PARMS((int));
  689. {
  690.     void        (*oldhandler) PARMS((int));
  691.  
  692.     assert(sig == SIGINT);
  693.  
  694.     oldhandler = Handler;
  695.     Handler = handler;
  696.     Chk_Abort();
  697.  
  698.     return oldhandler;
  699. }
  700.  
  701. /*
  702.  * This function should have the name that is used by your C library to
  703.  * poll CTRL-C.
  704.  */
  705.  
  706. long
  707. Chk_Abort PARMS((void))
  708. {
  709.     if (Handler == SIG_IGN) {
  710.     /*
  711.      * Don't clear the signal; if the user wants to abort we'll do
  712.      * that when it is allowed again.
  713.      */
  714.     return 0;
  715.     }
  716.     if ((SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)) {
  717.     void        (*oldhandler) PARMS((int));
  718.  
  719.     oldhandler = Handler;
  720.     Handler = SIG_DFL;
  721.     if (oldhandler == SIG_DFL) {
  722.         _abort();           /* must call zooexit() */
  723.         zooexit(20);        /* just in case */
  724.     }
  725.     (*oldhandler) (SIGINT);
  726.     return 1;
  727.     }
  728.     return 0;
  729. }
  730.  
  731. #endif
  732.